package com.mogist.hqc.module.product.ui

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.app.FragmentTransaction
import com.mogist.hqc.R
import com.mogist.hqc.base.BaseMVPActivity
import com.mogist.hqc.entitys.BaseModel
import com.mogist.hqc.entitys.response.ProductTypeVo
import com.mogist.hqc.module.product.contract.ProductListContract
import com.mogist.hqc.module.product.presenter.ProductListPresenter
import com.mogist.hqc.mvp.HttpDto
import com.mogist.hqc.util.Constant
import kotlinx.android.synthetic.main.activity_order_list.*
import kotlinx.android.synthetic.main.include_title.*

/**
 * 作用：玩家产品
 * 作者：赵小白 email:vvtale@gmail.com  
 * 修改人：
 * 修改时间：
 * 修改备注：
 */
class ProductListActivity : BaseMVPActivity<ProductListContract.Presenter>(), ProductListContract.View
{
    override fun initArgs(intent: Intent?)
    {
    }

    override fun initView()
    {
        setContentView(R.layout.activity_product_list)
        mPresenter = ProductListPresenter(this)
    }

    override fun initData()
    {
        mTitleBar.setTitle("玩家产品")
        getTypes()
    }

    private fun getTypes()
    {
        val body = BaseModel()
        mPresenter.getData(HttpDto(Constant.PRODUCT_TYPES,
                                   body,
                                   true).setType(HttpDto.GET).setShowError(false))
    }

    override fun bindTypes(list: ArrayList<ProductTypeVo>?)
    {
        initTabLayout(list)
    }

    // --------------- TabLayout ---------------
    private val fragmentList = ArrayList<Fragment?>()
    private val titleList = ArrayList<String>()
    private var mAdapter: TabFragmentAdapter? = null
    private var typeList: ArrayList<ProductTypeVo> = arrayListOf()
    private fun initTabLayout(list: ArrayList<ProductTypeVo>?)
    {
        //初始化标题栏数据和fragmentList
        titleList.add("推荐")
        fragmentList.add(null)
        mTabLayout.addTab(mTabLayout.newTab().setText("推荐"))

        list?.let {
            typeList = list
            for (type in list)
            {
                titleList.add(type.className)
                fragmentList.add(null)
                mTabLayout.addTab(mTabLayout.newTab().setText(type.className))
            }
        }
        mTabLayout.tabMode = TabLayout.MODE_SCROLLABLE //SCROLLABLE
        //实例化适配器
        mAdapter = TabFragmentAdapter(supportFragmentManager, mTabLayout.tabCount, titleList)
        mViewPager.adapter = mAdapter
        mTabLayout.setupWithViewPager(mViewPager) //必须在viewPager绑定适配器之后调用
        mTabLayout.setTabsFromPagerAdapter(mAdapter)
        mViewPager.offscreenPageLimit = titleList.size - 1
    }

    /** 自定义ViewPager适配器  */
    private inner class TabFragmentAdapter //构造函数，Fragment管理工具manger，标题栏个数nNumTab，标题栏文本List
    (private val manager: FragmentManager, private val tabNum: Int, private val titles: ArrayList<String>) : FragmentPagerAdapter(
            manager)
    {
        @SuppressLint("CommitTransaction")
        override fun getItem(position: Int): Fragment
        {
            //隐藏所有fragment
            hideFragments(manager.beginTransaction())
            if (fragmentList[position] == null)
            {
                val bundle = Bundle()
                bundle.putInt("position", position)
                if (position == 0)
                {
                    val fragment = Product0Fragment()
                    fragment.arguments = bundle
                    fragmentList[position] = fragment
                }
                else
                {
                    bundle.putString("code", typeList[position - 1].classCode)
                    val fragment = ProductFragment()
                    fragment.arguments = bundle
                    fragmentList[position] = fragment
                }
            }
            return fragmentList[position]!!
        }

        override fun getCount(): Int = tabNum

        /** 设置标题栏文本，必须复写  */
        override fun getPageTitle(position: Int): CharSequence = titles[position]
    }

    /** 隐藏所有fragment，提高复用率  */
    fun hideFragments(transaction: FragmentTransaction)
    {
        fragmentList.filterNotNull()
                .forEach { transaction.hide(it) }
    }

    /** 获取当前选中的Tab  */
    fun getCurr(): Int = mViewPager.currentItem
}